// $codepro.audit.disable

package edu.gatech.oad.project.testing;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import edu.gatech.oad.project.coordinator.Office;
import edu.gatech.oad.project.externalcommunication.IOConverter;
import edu.gatech.oad.project.operations.data.Appointment;
import edu.gatech.oad.project.operations.data.DoctorOrders;
import edu.gatech.oad.project.operations.data.PatientInformation;
import edu.gatech.oad.project.operations.data.TreatmentRecord;
import edu.gatech.oad.project.operations.scheduling.Schedule;
import edu.gatech.oad.project.operations.users.Doctor;
import edu.gatech.oad.project.operations.users.Nurse;
import edu.gatech.oad.project.operations.users.Patient;
import edu.gatech.oad.project.operations.users.UserCache;
import junit.framework.TestCase;


public class generalTesting extends TestCase{

   
    public void testAddPatient(){
        Office office = new Office();
        UserCache c = office.USERS;
        Map<String, List<Patient>> m = c.getPatientsInCache();
        
        office.addPatient("James Moore", 902576068, new PatientInformation());
        assertEquals(1, m.size());
        assertTrue("James Moore".equals(c.getPatient(902576068).getName()));
        
        office.addPatient("Nicole F", 11111111, new PatientInformation());
        assertEquals(2, m.size());
        assertTrue("Nicole F".equals(c.getPatient(11111111).getName()));
    }
    
    public void testUpdatePatient() {
        Office office = new Office();
        UserCache c = office.USERS;
        Map<String, List<Patient>> m = c.getPatientsInCache();
        
        office.addPatient("James Moore", 902576068, new PatientInformation());
        assertEquals(1, m.size());
        assertTrue("James Moore".equals(c.getPatient(902576068).getName()));
        assertEquals(0, c.getPatient(902576068).getInformation().getAge());
        
        PatientInformation newInfo = new PatientInformation();
        newInfo.setAge(24);
        office.updatePatient("James Moore", 902576068, "Jon Carmack", 555555555, newInfo);
        
        assertEquals(2, m.size());
        assertEquals(0, m.get("James Moore").size());
        assertTrue("Jon Carmack".equals(c.getPatient(555555555).getName()));
        assertEquals(24, c.getPatient(555555555).getInformation().getAge());
        assertEquals(555555555, c.getPatient(555555555).getID());
        assertEquals(null, c.getPatient(555555555).getInformation().getEmail());
    }
    
    public void testRemovePatient() {
        Office office = new Office();
        UserCache c = office.USERS;
        Map<String, List<Patient>> m = c.getPatientsInCache();
        
        office.addPatient("James Moore", 902576068, new PatientInformation());
        assertEquals(1, m.size());
        assertTrue("James Moore".equals(c.getPatient(902576068).getName()));
        
        office.removePatient("James Moore", 902576068);
        
        assertEquals(0, m.get("James Moore").size());
    }
    
    public void testGetPatient() {
        Office office = new Office();
        UserCache c = office.USERS;
        Map<String, List<Patient>> m = c.getPatientsInCache();
        
        office.addPatient("James Moore", 902576068, new PatientInformation());
        assertEquals(1, m.size());
        assertTrue("James Moore".equals(c.getPatient(902576068).getName()));
        
        PatientInformation newInfo = new PatientInformation();
        newInfo.setAge(24);
        office.updatePatient("James Moore", 902576068, "Jon Carmack", 555555555, newInfo);
   
        Patient temp = office.getPatient(555555555);
        
        assertEquals("Jon Carmack", temp.getName());
        assertEquals(555555555, temp.getID());
        assertEquals(24, temp.getInformation().getAge());
    }
    
    public void testMakeAppointments(){
        Office office = new Office();
        UserCache c = office.USERS;
        Map<String, List<Patient>> m = c.getPatientsInCache();
        
        PatientInformation newInfo = new PatientInformation();
        newInfo.setAge(24);
        office.addPatient("James Moore", 902576068, newInfo);
        assertEquals(1, m.size());
        assertTrue("James Moore".equals(c.getPatient(902576068).getName()));
        
        Patient tempP = office.getPatient(902576068);
        
        tempP.makeAppointment(20113107, 1630, new Doctor("Bob", 1), "to see if my code works!");
        List<Appointment> apps = tempP.getPastAppointments();
        
        assertEquals(1, apps.size());
        assertEquals(20113107, apps.get(0).getDate());
        assertEquals(1630, apps.get(0).getTime());
        assertEquals("to see if my code works!", apps.get(0).getReason());
        
    }
    
    public void testUpdateAppointment(){
        Office office = new Office();
        UserCache c = office.USERS;
        Map<String, List<Patient>> m = c.getPatientsInCache();
        
        PatientInformation newInfo = new PatientInformation();
        newInfo.setAge(24);
        office.addPatient("James Moore", 902576068, newInfo);
        assertEquals(1, m.size());
        assertTrue("James Moore".equals(c.getPatient(902576068).getName()));
        
        Patient tempP = office.getPatient(902576068);
        
        tempP.makeAppointment(20113107, 1630, new Doctor("Bob", 1), "to see if my code works!");
        
        Appointment p = new Appointment(1,20113107, 1630, null, null, null);
        Appointment app = tempP.getAppointment(p, false);
        assertFalse(app == p);
        app.setReason("this is the new reason");
        app = tempP.getAppointment(new Appointment(1,20113107, 1630, null, null, null), false);
        assertEquals("this is the new reason", app.getReason());
        assertEquals(app.getPat().getName(), "James Moore");
    }
    
      
    public void testDeleteAppointment(){
        Office office = new Office();
        UserCache c = office.USERS;
        Map<String, List<Patient>> m = c.getPatientsInCache();
        
        PatientInformation newInfo = new PatientInformation();
        newInfo.setAge(24);
        office.addPatient("James Moore", 902576068, newInfo);
        assertEquals(1, m.size());
        assertTrue("James Moore".equals(c.getPatient(902576068).getName()));
        
        Patient tempP = office.getPatient(902576068);
        
        tempP.makeAppointment(20113107, 1630, new Doctor("Bob", 1), "to see if my code works!");
        
        int num = tempP.getAppointment(new Appointment(0, 20113107, 1630, new Doctor("Bob", 1), tempP, "to see if my code works!"), false).getID();
        
        Appointment p = new Appointment(num, 20113107, 1630, new Doctor("Bob", 1), tempP, "to see if my code works!");
        tempP.removeAppointment(p);
        
        List<Appointment> fe = tempP.getPastAppointments();
        assertEquals(fe.size(), 0);
    }
    
    public void testMakeTreatmentRecord(){
        TreatmentRecord record = new TreatmentRecord();
        Appointment app = new Appointment(1,2011317, 1630, new Doctor("Bob", 1),
                new Patient("James Moore", 1), "to see if my code works!");
        
        app.addRecord(record);
        
        record.setDiagnosis("dead");

        app.updateRecord(record);
        
        assertEquals("dead", app.getRecord().getDiagnosis());
    }
    
    public void testUpdateTreatementRecord(){
        TreatmentRecord record = new TreatmentRecord();
        Appointment app = new Appointment(1,2011317, 1630, new Doctor("Bob", 1),
                new Patient("James Moore", 1), "to see if my code works!");
        
        app.addRecord(record);
        
        record.setDiagnosis("dead");

        app.updateRecord(record);
        
        assertEquals("dead", app.getRecord().getDiagnosis());
        
        TreatmentRecord tempR = new TreatmentRecord();
        tempR.setDiagnosis("not dead");
        
        app.getRecord().update(tempR);
        
        assertEquals("not dead", app.getRecord().getDiagnosis());
    }
    
    public void testUpdateTreatmentRecord2()
    {
        TreatmentRecord record = new TreatmentRecord();
        TreatmentRecord temp = new TreatmentRecord();
        
//        temp.setDoc(new Doctor("Sam",44)); 
//        record.update(temp);
//        assertEquals(record.getDoc().getName(),"Sam");
        
//        temp.setTime(new Date(3));
//        record.update(temp);
//        assertEquals(record.getTime(),new Date(3));
        
        temp.setAttendingNurse(new Nurse("Laura",363));
        record.update(temp);
        assertEquals(record.getAttendingNurse().getName(),"Laura");
        
        DoctorOrders d = new DoctorOrders();
        List<String> s = new ArrayList<String>();
        s.add("pretend instuctions");
        d.setInstructions(s);
        temp.setOrders(d);
        record.update(temp);
        //assertTrue(record.getOrders().getInstructions().get(0).equals("pretend instructions"));
        
        temp.setDiagnosis("dead");
        record.update(temp);
        assertEquals(record.getDiagnosis(),"dead");
        
        List<String> symp = new ArrayList<String>();
        symp.add("fever");
        temp.setSymptoms(symp);
        record.update(temp);
        assertTrue(record.getSymptoms().get(0).equals("fever"));
        
        Map<String,Integer> vital = new HashMap<String, Integer>();
        vital.put("Heartbeat", 155);
        temp.setVitalSigns(vital);
        record.update(temp);
        assertTrue(record.getVitalSigns().get("Heartbeat") == 155);
        assertTrue(record.getVitalSigns().containsKey("Heartbeat") == true);
    }
   
    public void testDeleteTreatmentRecord(){
        TreatmentRecord record = new TreatmentRecord();
        Appointment app = new Appointment(1,2011317, 1630, new Doctor("Bob", 1),
                new Patient("James Moore", 1), "to see if my code works!");
        
        app.addRecord(record);
        
        record.setDiagnosis("dead");
        app.updateRecord(record);
        
        assertEquals("dead", app.getRecord().getDiagnosis());
        
        
        app.addRecord(null);
        
        assertEquals(null, app.getRecord());
    }
    
    public void testAddDoctorsOrders(){
        TreatmentRecord record = new TreatmentRecord();
        Appointment app = new Appointment(1,2011317, 1630, new Doctor("Bob", 1),
                new Patient("James Moore", 1), "to see if my code works!");
        
        app.addRecord(record);
        
        record.setDiagnosis("dead");
       
        app.updateRecord(record);
        
        DoctorOrders o = new DoctorOrders();
        app.addDocOrders(o);
        List<String> s = new ArrayList<String>();
        s.add("for testing");
        o.setInstructions(s);
        
        assertTrue(app.getDocOrders().getInstructions().get(0).equals("for testing"));
    }
    
    public void testUpdateDoctorsOrders(){
        TreatmentRecord record = new TreatmentRecord();
        Appointment app = new Appointment(1,2011317, 1630, new Doctor("Bob", 1),
                new Patient("James Moore", 1), "to see if my code works!");
        
        app.addRecord(record);
        
        record.setDiagnosis("dead");

        app.updateRecord(record);
        
        DoctorOrders o = new DoctorOrders();
        app.addDocOrders(o);
        List<String> s = new ArrayList<String>();
        s.add("for testing");
        o.setInstructions(s);
        
        List<String> ns = new ArrayList<String>();
        DoctorOrders o2 = new DoctorOrders();
        ns.add("new");
        o.setInstructions(ns);
        
        app.updateDocOrders(o2);
        
        assertTrue(app.getDocOrders().getInstructions().get(0).equals("new"));
    }
    
    public void testDeleteDoctorOrders(){
        TreatmentRecord record = new TreatmentRecord();
        Appointment app = new Appointment(1,201131047, 1630, new Doctor("Bob", 1),
                new Patient("James Moore", 1), "to see if my code works!");
        
        app.addRecord(record);
        
        record.setDiagnosis("dead");
        System.out.println(record.getDiagnosis());
        app.updateRecord(record);
        
        DoctorOrders o = new DoctorOrders();
        app.addDocOrders(o);
        List<String> s = new ArrayList<String>();
        s.add("for testing");
        o.setInstructions(s);
        
        assertTrue(app.getDocOrders().getInstructions().get(0).equals("for testing"));
        
        app.getRecord().setOrders(null);
        
        assertEquals(app.getRecord().getOrders(), null);
    }
    
    public void testConnectionAppointment(){
        IOConverter c = new IOConverter();
        //Appointment app = new Appointment(3,19910424, 1630, new Doctor("Bob", 2),
               // new Patient("James Moore", 4), "to see if my code works!");
        Patient p = new Patient("James Moore", 4, new Schedule(c));
        p.makeAppointment(20000424, 1050, new Doctor("Bob", 2), "second test");
        //c.addAppointment(app);

        //c.deleteAppointment(app);
        Appointment app = p.getAppointment(new Appointment(10, 19940424, 1050, null, null, "l"), false);
        //for (int i = 0; i < 0; i++) {
        //    app.setID(i);
        //    c.deleteAppointment(app);
            
        //}
        app.setReason("PYTHON IS AWESOME");
        app.confirm();
        //c.updateAppointment(app);
    }
    
    public void testLogin() {
        IOConverter c = new IOConverter();
        
        c.execute("update users set attempts = '0' where username = 'jmoore64'");
        c.execute("update users set active = 1 where username = 'jmoore64'");
        
        String[] vals = c.login("jmoore64", "awesome");
        assertEquals("true", vals[0]);
        assertEquals("1", vals[1]);
        assertEquals("admin", vals[2]);
        
        vals = c.login("jmoore64", "incorrect");
        assertEquals("false", vals[0]);
        assertEquals("1", vals[1]);
        assertEquals("password", vals[2]);
        
        c.execute("update users set attempts = '3' where username = 'jmoore64'");
        
        vals = c.login("jmoore64", "awesome");  
        assertEquals("false", vals[0]);
        assertEquals("3", vals[1]);
        assertEquals("attempt", vals[2]);
        
        vals = c.login("jmoore64", "incorrect");  
        assertEquals("false", vals[0]);
        assertEquals("3", vals[1]);
        assertEquals("attempt", vals[2]); 
        
        c.execute("update users set active = 0 where username = 'jmoore64'");
        
        vals = c.login("jmoore64", "awesome");  
        assertEquals("false", vals[0]);
        assertEquals("-1", vals[1]);
        assertEquals("inactive", vals[2]);
        
        vals = c.login("not a name", "awesome");  
        assertEquals("false", vals[0]);
        assertEquals("-1", vals[1]);
        assertEquals("username", vals[2]); 
        
        c.execute("update users set attempts = '0' where username = 'jmoore64'");
        c.execute("update users set active = 1 where username = 'jmoore64'");
    }
    
    public void testUpdateMedical(){
    	PatientInformation p = new PatientInformation();
    	PatientInformation o = new PatientInformation();
    	
    	List<String> s = new ArrayList<String>();
        List<String> t = new ArrayList<String>();
    	
        List<String> oldMisc = new ArrayList<String>();
        List<String> newMisc = new ArrayList<String>();
        	
        
        
        s.add("Bees!");
    	t.add("Poison Sumac!!");
    	
    	oldMisc.add("Caution: I may spontaneously combust!");
    	newMisc.add("Doc, it hurts when I do this...");
    	
    	p.setMiscMedicalData(oldMisc);
    	o.setMiscMedicalData(newMisc);
    	
    	p.setAllergies(s);
    	o.setAllergies(t);
    	
    	p.setPharmacy("Walgreens");
    	p.setInsuranceCarrier("Blue Cross, Blue Shield");
    	
    	o.setPharmacy("Rite-Aid");
    	o.setInsuranceCarrier("Tricare");
    	
    	p.updateMedical(o);
    	
    	assertEquals("Rite-Aid", p.getPharmacy());
    	assertEquals("Tricare", p.getInsuranceCarrier());
    	assertEquals("Poison Sumac!!", p.getAllergies().get(0));
    	assertEquals("Doc, it hurts when I do this...", p.getMiscMedicalData().get(0));

    }
    
    public void generalSQLTesting() {
        DoctorOrders orders = new DoctorOrders();
        
        List<String> i = new ArrayList<String>();
        i.add("do a hand stand");
        i.add("sleep");
        i.add("stop going to Georgia Tech");
        orders.setInstructions(i);
        
        List<String> l = new ArrayList<String>();
        l.add("blood analysis");
        l.add("lolwut, work?");
        orders.setLabWorkPerformed(l);
        
        List<String> p = new ArrayList<String>();
        p.add("two advil per day");
        p.add("what you want more drugs?");
        p.add("druggie");
        orders.setPrescription(p);
        
        TreatmentRecord record = new TreatmentRecord();
        record.setAttendingNurse(new Nurse("name", 3));
        record.setDiagnosis("only partially dead");
        record.setOrders(orders);
        
        List<String> s = new ArrayList<String>();
        s.add("swolen leg");
        s.add("rash");
        record.setSymptoms(s);
        
        Map<String, Integer> v = new HashMap<String, Integer>();
        v.put("Bored Level", 9001);
        v.put("Blood Pressure", 1);
        v.put("BP top", 2);
        v.put("Hammer Time", 501);
        v.put("no creativity", 8);
        record.setVitalSigns(v);
        
        Appointment app = new Appointment(0, 20110424, 1030, new Doctor("name", 2), new Patient("name", 4), "java <= everything");
        app.addRecord(record);
        IOConverter sql = new IOConverter();
        //int id = sql.addAppointment(app);
        //sql.addTreatmentRecord(app.getRecord(), id);
        //sql.addDoctorOrders(app.getDocOrders(), id);
        
        TreatmentRecord ne = new TreatmentRecord();
        v = new HashMap<String, Integer>();
        v.put("OMG NEW STUFF", 9001);
        ne.setVitalSigns(v);
        ne.setDiagnosis("nope now you are fully dead");
        //sql.updateRecord(ne, id);
        sql.deleteRecord(67);
        
    }
    
    public void registerTesting() {
        boolean fail;
        IOConverter db = new IOConverter();
        
        fail = db.canRegister("jmoore64", "lolwut", "nurse");
        assertEquals(fail, false);
        
        fail = db.canRegister("THEAWESOMEPATIENT", "1337", "patient");
        assertEquals(fail, true);
    }
}
